package com.intct.hbase.ddl.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-08-04 15:26
 * 连接HBase
 */
public class HBaseConnection {

    // 重量级连接、线程安全、建议池化
    private static Connection connection = null;

    static {
        // 连接HBase
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "cdh-node:2181");
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() {
        return connection;
    }

    public static void closeConnect(Connection connection) throws IOException {
        if (connection != null) {
            connection.close();
        }
    }

    public static void main(String[] args) {
        Connection conn = getConnection();
        System.out.println(conn);
    }
}
